/** 登陆 **/
import router from '@/router'
import store from '@/store'
import messageBox from '@/utils/messageBox.js'
import Util from '@/utils/test.js'
import api from '@/api'
import { storage, createStorage } from '@/utils/storage'
const sessStorage = createStorage({ storage: sessionStorage })
import { ACCESS_TOKEN, CURRENT_USER, CURRENT_SHARE_CODE } from '@/store/mutation-types'
const state = {
  info: storage.get(CURRENT_USER, null),
  token: storage.get(ACCESS_TOKEN, null),
  // 用户礼包
  ticket: {
    visible: false,
    newUser: 0, // 是否是新用户
    list: []
  }
}
const mutations = {
  inInfo(state, data) {
    state.info = data
  },
  // 打开/关闭用户礼包
  useUserTicketDialog(state, status) {
    setTimeout(() => {
      state.ticket.visible = status
    }, 2000)
  }
}

const actions = {
  /**
   * @description 登录
   * @param {Object} data 用户信息
   */
  verifyLogin({ state, dispatch, commit }, data) {
    return new Promise((resolve, reject) => {
      api.createAccountLogin(
        data,
        res => {
          // 用户token
          state.token = res.data.auth_token
          if (!Util.isEmpty(res.data.ticket)) {
            // 显示用户礼包
            commit('useUserTicketDialog', true)

            // 用户礼包数据
            state.ticket.list = res.data.ticket
          }
          storage.set(ACCESS_TOKEN, res.data.auth_token)
          dispatch('userInfo')

          store.dispatch('projectSetting/setGlobCartNumber')
          store.dispatch('projectSetting/setGlobNotifyUnread')

          resolve(res)
        },
        error => {
          reject(error)
        }
      )
    })
  },
  /**
   * @description 绑定第三方账号
   * @param {Object} data 用户信息
   */
  thirdlogin({ state, dispatch, commit }, data) {
    state.token = data.auth_token
    if (!Util.isEmpty(data.ticket)) {
      // 显示用户礼包
      commit('useUserTicketDialog', true)

      // 用户礼包数据
      state.ticket.list = data.ticket
    }
    storage.set(ACCESS_TOKEN, data.auth_token)
    dispatch('userInfo')
    store.dispatch('projectSetting/setGlobCartNumber')
    store.dispatch('projectSetting/setGlobNotifyUnread')
  },
  /**
   * @description 注册
   * @param {Object} data 用户信息
   */
  accountRegister({ state, dispatch, commit }, data) {
    return new Promise((resolve, reject) => {
      api.createAccountReg(
        data,
        res => {
          // 用户token
          state.token = res.data.auth_token
          if (!Util.isEmpty(res.data.ticket)) {
            // 显示用户礼包
            commit('useUserTicketDialog', true)

            // 新用户
            state.ticket.newUser = 1

            // 用户礼包数据
            state.ticket.list = res.data.ticket
          }
          storage.set(ACCESS_TOKEN, res.data.auth_token)
          dispatch('userInfo')
          store.dispatch('projectSetting/setGlobCartNumber')
          store.dispatch('projectSetting/setGlobNotifyUnread')
          resolve(res)
        },
        error => {
          reject(error)
        }
      )
    })
  },

  /**
   * @description 注销用户并返回登录页面
   */
  logout({ state, dispatch }, confirm = false) {
    if (confirm) {
      messageBox
        .confirm('确定退出登录?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        .then(() => {
          dispatch('compelLogout')
          // 跳转路由
          router.push({
            path: store.getters['projectSetting/getGlobLoginConfigLink']
          })
        })
    }
  },
  //
  /**
   * @description 登录失效 强制清出token
   */
  compelLogout({ state }) {
    // 清空token
    state.token = null
    storage.remove(ACCESS_TOKEN)
    // 清空用户信息
    state.info = null
    storage.remove(CURRENT_USER)
    // 删除分享的code
    sessStorage.remove(CURRENT_SHARE_CODE)

    store.commit('projectSetting/setCartNumber', 0)
    store.commit('projectSetting/setNotifyUnread', 0)
  },
  /**
   * @description 获取用户信息
   */
  userInfo({ commit }) {
    api.getAccountInfo(res => {
      const { data } = res
      commit('inInfo', data)
      storage.set(CURRENT_USER, data)
    })
  },
  /**
   * @description 获取用户礼包
   */
  userTicket({ state, commit }) {
    api.getAccountTicket(res => {
      const { data } = res
      if (!Util.isEmpty(data)) {
        // 显示用户礼包
        commit('useUserTicketDialog', true)
        // 用户礼包数据
        state.ticket.list = data
      }
    })
  }
}

const getters = {
  isLogin(state) {
    if (Util.isDef(state.token) && !Util.isEmpty(state.token)) {
      return true
    }
    return false
  },
  token(state) {
    if (Util.isDef(state.token)) return state.token
    return {
      ak: '',
      sK: ''
    }
  },
  accountInfo(state) {
    if (Util.isDef(state.info)) return state.info
    return {}
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
